﻿@using GlobalResources
@using Microsoft.Azure.IoTSuite.Connectedfactory.WebApp.Models
@using Microsoft.Azure.IoTSuite.Connectedfactory.WebApp.Security
@using Microsoft.Azure.IoTSuite.Connectedfactory.WebApp.Contoso

@model OpcSessionModel


<div class="container-fluid browser_browse_container">
    <div class="row browser_container_header">
        <div class="browser_container_header_text overflow">@Strings.BrowserConnectedTo : @Model.EndpointUrl</div>
    </div>
    <div class="row browser_top_container">
        <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6">
            <div id="OpcNodeTree" class="browser_browse_nodetree_container">
                <ul>
                    <li id="rootnode">
                        @Model.EndpointUrl
                        <ul>
                            <li></li>
                        </ul>
                    </li>
                </ul>
            </div>
            <div class="row browser_browse_disconnect_button">
                @Html.ActionLink(@Strings.ButtonBack, "Back", "Browser", new { backUrl = @Model.BackUrl }, new { @class = "btn btn-default btn_browser" })
            </div>
        </div>
        <div id="browserImage" class="browser_image_container  col-xs-12 col-sm-12 col-md-5 col-lg-5">
            <img aria-label="image" class="img-responsive" style="width:100%;" src="~/Content/img/packing_station.png" />
        </div>
    </div>

    @*To enable display of connection messages uncomment the following lines*@
    @*<div class="row browser_browse_status_message_container">
        <p id="message"> </p>
    </div>*@

    <div id="attributeReadContextPanel" class="overlay">
        <div class="dashboard_contextpanel_header">
            <div class="dashboard_contextpanel_header_text">@Strings.BrowserContextPanelHeaderAttributes</div>
        </div>
        <a aria-label="closePanel" href="#" class="closebtn" onclick="$('#attributeReadContextPanel').addClass('overlay-close')" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
        <div class="overlay-content list-group">
            <div class="list-group-item text-left list-group-item-heading"><b>@Strings.NodeAttribute <span id="attributeRead_variableName">@Strings.NodeVariableName</span></b></div>
            <hr />
            <span class="list-group-item text-left" id="attributeRead_variableData">@Strings.NodeReadInProgress</span>
        </div>
    </div>

    <div id="variableReadContextPanel" class="overlay">
        <div class="dashboard_contextpanel_header">
            <div class="dashboard_contextpanel_header_text">@Strings.BrowserContextPanelHeaderRead</div>
        </div>
        <a aria-label="closePanel" href="#" class="closebtn" onclick="$('#variableReadContextPanel').addClass('overlay-close')" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
        <div class="overlay-content list-group">
            <div class="list-group-item text-left list-group-item-heading"><b>@Strings.NodeInfo <span id="variableRead_variableName">@Strings.NodeVariableName</span></b></div>
            <hr />
            <span class="list-group-item text-left" id="variableRead_variableData">@Strings.NodeReadInProgress</span>
        </div>
        <div id="methodCallShowErrorRead" class="overlay-content list-group" style="display:none">
            <div class="list-group-item text-left list-group-item-heading" id="errorInfo_header_read"><b></b></div>
            <div class="list-group-item text-left" id="errorInfo_message_read"></div>
        </div>
    </div>

    <div id="variablePublishContextPanel" class="overlay">
        <div class="dashboard_contextpanel_header">
            <div id="publishHeader" class="dashboard_contextpanel_header_text">@Strings.BrowserContextPanelHeaderPublish</div>
        </div>
        <a aria-label="closePanel" href="#" class="closebtn" onclick="$('#variablePublishContextPanel').addClass('overlay-close')" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
        <div id="waitingForPublishing" class="browser_error_details_container">@Strings.BrowserContextPanelPublishing</div>
        <div id="connectLoader" class="loader"></div>
        <div id="publishBody" class="overlay-content list-group">
            <div class="list-group-item text-left list-group-item-heading"><b>@Strings.NodeInfo <span id="variablePublish_variableName">@Strings.NodeVariableName</span></b></div>
            <hr />
            <span class="list-group-item text-left" id="variablePublish_variableData">@Strings.NodePublishInProgress</span>        
        </div>
        <div id="methodCallShowErrorPublish" class="overlay-content list-group" style="display:none">
            <div class="list-group-item text-left list-group-item-heading" id="errorInfo_header_publish"><b></b></div>
            <div class="list-group-item text-left" id="errorInfo_message_publish"></div>
        </div>
        <div id="rdxLink" class="browser_rdxlink_container" onclick="openRDXExplorerLink(linkRDXExplorer)">@Strings.Tsilink</div>
    </div>

    <div id="methodCallContextPanel" class="overlay">
        <div class="overlay-scroll scrollbar-macosx">
            <div class="dashboard_contextpanel_header">
                <div class="dashboard_contextpanel_header_text">@Strings.BrowserContextPanelHeaderMethod</div>
            </div>
            <a aria-label="closePanel" href="#" class="closebtn" onclick="$('#methodCallContextPanel').addClass('overlay-close')" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
            <div class="browser_error_details_container">
                <div class="row browser_error_details_line_header">
                    <span class="glyphicon glyphicon-alert"></span>
                    <span class="browser_error_details_label">@Strings.BrowserWriteWarningHeader</span>
                </div>
                <div class="row browser_error_details_line">
                    <span class="browser_error_details_info_black">@Strings.BrowserWriteWarning</span>
                </div>
            </div>
            <div class="overlay-content list-group">
                <div class="list-group-item text-left"><b>@Strings.NodeCallingMethod <span id="methodCall_methodName">@Strings.NodeVariableName</span></b></div>
                <p id="methodCall_methodId" hidden></p>
                <p id="methodCall_parameterOriginalData" hidden></p>
                <div class="list-group-item text-left">
                    <form class="form-inline" role="form" id="variableMethodCall">
                        <div class="form-group context-panel-data">
                            <span id="methodCall_parameterData">@Strings.NodeMethodMessage</span>
                        </div>
                        <hr />
                        <div class="node-contextPanel-button-position">
                            <button onclick="MethodCall_CallButton_Click(this)" type="button" class="btn btn-default btn_browser_contextpanel">@Strings.NodeButtonCall</button>
                        </div>
                    </form>
                </div>
            </div>
            <div id="methodCallShowResult" class="overlay-content list-group" style="display:none">
                <div class="list-group-item text-left"><b>@Strings.NodeMethodResult <span id="methodCallResult_methodName">@Strings.NodeMethodName</span></b></div>
                <span class="list-group-item text-left" id="methodCallResult_result">@Strings.NodeCallInProgress</span>
            </div>
            <div id="methodCallShowErrorMethodCall" class="overlay-content list-group" style="display:none">
                <div class="list-group-item text-left list-group-item-heading" id="errorInfo_header_methodCall"><b></b></div>
                <div class="list-group-item text-left" id="errorInfo_message_methodCall"></div>
            </div>
        </div>
    </div>

    <div id="variableWriteContextPanel" class="overlay">
        <div class="overlay-scroll scrollbar-macosx">
            <div class="dashboard_contextpanel_header">
                <div class="dashboard_contextpanel_header_text">@Strings.BrowserContextPanelHeaderWrite</div>
            </div>
            <a aria-label="closePanel" href="#" class="closebtn" onclick="$('#variableWriteContextPanel').addClass('overlay-close')" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
            <div class="browser_error_details_container">
                <div class="row browser_error_details_line_header">
                    <span class="glyphicon glyphicon-alert"></span>
                    <span class="browser_error_details_label">@Strings.BrowserWriteWarningHeader</span>
                </div>
                <div class="row browser_error_details_line">
                    <span class="browser_error_details_info_black">@Strings.BrowserWriteWarning</span>
                </div>
            </div>
            <div class="overlay-content list-group">
                <div class="list-group-item text-left list-group-item-heading"><b>@Strings.NodeUpdate <span id="variableWrite_variableName">@Strings.NodeVariableName</span></b></div>
                <p id="variableWrite_variableId" hidden></p>
                <hr />
                <div class="list-group-item text-left">
                    <form class="form-inline" role="form" id="variableWrite">
                        <div class="form-group context-panel-data">
                            <b>@Strings.NodeWriteCurrentValue</b><br />
                            <span id="variableWrite_variableOldData">@Strings.NodeWriteInProgress</span><br /><br />
                            <label>@Strings.NodeWriteNewValue</label>
                            <input aria-label="inputData" id="variableWrite_variableNewData" type="text" class="form-control">
                        </div>
                        <hr />
                        <div class="node-contextPanel-button-position">
                            <button onclick="VariableWrite_WriteButton_Click(this)" type="button" class="btn btn-default btn_browser_contextpanel">@Strings.NodeButtonWrite</button>
                        </div>
                    </form>
                </div>
            </div>
            <div id="variableWriteShowResult" class="overlay-content list-group" style="display:none">
                <div class="list-group-item text-left"><b>@Strings.NodeWriteResult <span id="variableWriteResult_variableName">@Strings.NodeVariableName</span></b></div>
                <span class="list-group-item text-left" id="variableWriteResult_result">@Strings.NodeWriteInProgress</span>
            </div>
            <div id="methodCallShowErrorWrite" class="overlay-content list-group" style="display:none">
                <div class="list-group-item text-left list-group-item-heading" id="errorInfo_header_write"><b></b></div>
                <div class="list-group-item text-left" id="errorInfo_message_write"></div>
            </div>
        </div>
    </div>

    <div id="browserErrorContextPanel" class="overlay">
        <div class="dashboard_contextpanel_header">
            <div class="dashboard_contextpanel_header_text">Error</div>
        </div>
        <a aria-label="closePanel" href="#" class="closebtn" onclick="closeErrorPanel()" role="button" ><img aria-label="closePanelIcon" class="dashboard-cancel-icon" src="~/Content/img/cancel.svg" hidden></a>
        <div class="overlay-content">
            <div class="browser_error_contextpanel_container">
                <div class="browser_error_contextpanel_header">@Strings.BrowserError</div>
                <div id="browserErrorMessage" class="browser_error_contextpanel_message">@Strings.BrowserErrorUnknown</div>
            </div>
        </div>
    </div>

</div>

@section scripts {
    <script src="~/Scripts/jstree.min.js"></script>
    <script src="/signalr/hubs"></script>
    <script>
        var title = "@Strings.TitleBrowser";

        $(function () {
            var status = $.connection.statusHub;

            // Create a function that the hub can call back to display messages
            status.client.addNewMessageToPage = function (session, message) {
                if (session === '@Session.SessionID') {
                    $('#message').replaceWith('<p id="message">@Strings.BrowserCommunicationStatusMessageLabel' + ' ' + htmlEncode(message) + '</p>');
                }
            }

            // Start/continue the connection
            $.connection.hub.start().done(function () { });

            // initialize all context panels to default state closed
            $("#attributeReadContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
            $("#variableReadContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
            $("#variablePublishContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
            $("#methodCallContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
            $("#variableWriteContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
            $("#browserErrorContextPanel").addClass("overlay-close").removeClass("overlay-right-open");
        });

        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }

        function htmlDecode(value) {
            decodedValue = $('<div>').html(value).text();
            return decodedValue;
        }

        function VariableRead(obj) {
            $.post({
                url: "/Browser/VariableRead",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: obj.item.nodeId },
                success: function (data) {
                    $("#variableRead_variableData").html(data);
                    $("#variableRead_variableName").html(obj.item.nodeName);

                    if (data.errorInfo != null) {
                        $("#errorInfo_message_read").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_read").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorRead").show();
                    } else {
                        $("#methodCallShowErrorRead").hide();
                    }

                    $(".overlay").addClass("overlay-close", function () {
                        $("#variableReadContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
                        $(".dashboard-cancel-icon").show();
                    });
                },
                error: function (response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        var linkRDXExplorer

        function VariablePublish(obj) {
            $.post({
                url: "/Browser/VariablePublishUnpublish",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: obj.item.nodeId, method: "publish" },
                success: function (data) {
                    $("#variablePublish_variableData").html(data);
                    $("#variablePublish_variableName").html(obj.item.nodeName);
                    $("#connectLoader").hide();
                    $("#waitingForPublishing").hide();
                    $("#publishBody").show();
                    if (data == htmlDecode('@Strings.BrowserOpcPublishSucceeded')) {
                        var jsonNode = $('#OpcNodeTree').jstree(true).get_node(obj.item.nodeId);
                        $('#OpcNodeTree').jstree(true).set_type(jsonNode, "published");
                        jsonNode.original.publishedNode = true;
                        $("#rdxLink").show();
                        linkRDXExplorer("@ContosoTopologyNode.AggregationView.Hour", null).done(function(rdxUrl) {
                            linkRDXExplorer = rdxUrl;
                        });
                    }
                    if (data.errorInfo != null) {
                        $("#errorInfo_message_publish").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_publish").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorPublish").show();
                    } else {
                        $("#methodCallShowErrorPublish").hide();
                    }
                },
                error: function (response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function VariableUnpublish(obj) {
            $.post({
                url: "/Browser/VariablePublishUnpublish",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: obj.item.nodeId, method: "unpublish" },
                success: function (data) {
                    $("#variablePublish_variableData").html(data);
                    $("#variablePublish_variableName").html(obj.item.nodeName);
                    $("#connectLoader").hide();
                    $("#waitingForPublishing").hide();
                    $("#publishBody").show();
                    if (data == htmlDecode('@Strings.BrowserOpcUnPublishSucceeded')) {
                        var jsonNode = $('#OpcNodeTree').jstree(true).get_node(obj.item.nodeId);
                        $('#OpcNodeTree').jstree(true).set_type(jsonNode, "default");
                        jsonNode.original.publishedNode = false;
                    }
                    if (data.errorInfo != null) {
                        $("#errorInfo_message_publish").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_publish").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorPublish").show();
                    } else {
                        $("#methodCallShowErrorPublish").hide();
                    }
                },
                error: function (response) {
                    openErrorPanel();
                    $("#rdxLink").hide();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function VariableWrite(obj) {
            $.post({
                url: "/Browser/VariableWriteFetch",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: obj.item.nodeId },
                success: function(data) {
                    $("#variableWrite_variableOldData").html(data);
                    $("#variableWrite_variableName").text(obj.item.nodeName);
                    $("#variableWrite_variableId").text(obj.item.nodeId);
                    $(".overlay").addClass("overlay-close", function() {
                        $("#variableWriteContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
                        $("#variableWriteShowResult").hide();
                        $(".dashboard-cancel-icon").show();
                    });

                    if (data.errorInfo != null) {
                        $("#errorInfo_message_write").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_write").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorWrite").show();
                    } else {
                        $("#methodCallShowErrorWrite").hide();
                    }
                },
                error: function(response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function VariableWrite_WriteButton_Click() {
            $("#variableWriteShowResult").show();

            var newVal = $("#variableWrite_variableNewData").val();
            var id = $("#variableWrite_variableId").text();
            $("#variableWriteResult_result").html("@Strings.NodeWriteInProgress");
            $("#variableWriteResult_variableName").text($("#variableWritevariableName").text());
            $.post({
                url: "/Browser/VariableWriteUpdate",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: id, newValue: newVal },
                success: function (data) {
                    $("#variableWriteResult_result").html(data);
                    if (data.errorInfo != null) {
                        $("#errorInfo_message_write").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_write").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorWrite").show();
                    } else {
                        $("#methodCallShowErrorWrite").hide();
                    }
                },
                error: function (response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function MethodCall(obj) {
            $.post({
                url: "/Browser/MethodCallGetParameter",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: obj.item.nodeId },
                success: function (data) {
                    if (data.count === 0) {
                        $("#methodCall_parameterData").html("@Strings.NodeMethodNoParameter" + "<br/>" + "@Strings.NodePressButton" + "<br/>");
                    } else {
                        $("#methodCall_parameterData").html("");
                        for (var i = 0; i < data.count; i++) {
                            var currentHtml = $("#methodCall_parameterData").html();
                            var parameterHtml = "<b>" + data.parameter[i].name + "</b>" + ": ";
                            parameterHtml += "<br/>@Strings.NodeLabelType" + ": " + data.parameter[i].typename + ";<br />" + "@Strings.NodeLabelDescription " + data.parameter[i].description + "<br/><br/>";
                            parameterHtml += "<input aria-label=\"inputData\" id=\"methodCall_parameterData_" + i + "\" type=\"text\" class=\"form-control\"><br/><br/>"
                            $("#methodCall_parameterData").html(currentHtml + parameterHtml);
                        }
                    }
                    if (data.errorInfo != null) {
                        $("#errorInfo_message_methodCall").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_methodCall").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorMethodCall").show();
                    } else {
                        $("#methodCallShowErrorMethodCall").hide();
                    }

                    $("#methodCall_parameterOriginalData").text(JSON.stringify(data));
                    $("#methodCall_methodName").text(obj.item.nodeName);
                    $("#methodCall_methodId").text(obj.item.nodeId);
                    $(".overlay").addClass("overlay-close", function () {
                        $("#methodCallContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
                        $("#methodCallShowResult").hide();
                        $(".dashboard-cancel-icon").show();
                    });
                },
                error: function (response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function MethodCall_CallButton_Click() {
            $("#methodCallShowResult").hide();

            var jsonString = $("#methodCall_parameterOriginalData").text();
            var parameterOriginalData = "";
            if (jsonString !== "") {
                parameterOriginalData = JSON.parse(jsonString);
            } else {
                parameterOriginalData.parameter = "";
            }
            var id = $("#methodCall_methodId").text();
            var parameterValues = [];
            var parameterVal;
            var parameterValEncoded;
            var parameterId;
            for (var i = 0; parameterOriginalData != undefined && i < parameterOriginalData.count; i++) {
                parameterId = "#methodCall_parameterData_" + i;
                parameterVal = $(parameterId).val();
                parameterValEncoded = $(parameterId).text(parameterVal).html();
                parameterValues.push(parameterValEncoded);
            }
            $("#methodCallResult_result").text("@Strings.NodeCallInProgress");
            $("#methodCallResult_methodName").text($("#methodCall_methodName").text());
            $.post({
                url: "/Browser/MethodCall",
                data: { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: id, parameterData: JSON.stringify(parameterOriginalData.parameter), parameterValues: JSON.stringify(parameterValues), session: null },
                success: function (data) {
                    $("#methodCallResult_result").html(data);
                    $("#methodCallShowResult").show();
                    if (data.errorInfo != null) {
                        $("#errorInfo_message_methodCall").html(data.errorInfo.errorMessage);
                        $("#errorInfo_header_methodCall").html('@Strings.BrowserErrorInfo');
                        $("#methodCallShowErrorMethodCall").show();
                    } else {
                        $("#methodCallShowErrorMethodCall").hide();
                    }
                },
                error: function (response) {
                    openErrorPanel();
                    if (response.status == 403) {
                        $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                    } else {
                        $('#browserErrorMessage').html(response.responseText);
                    }
                }
            });
        }

        function openErrorPanel() {
            $("#browserErrorContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
            $("#disconnectButton").attr("disabled", "disabled");
            $(".dashboard-cancel-icon").show();
            $(".jstree-ocl").hide();
            $(".jstree-anchor").hide();
        }

        function closeErrorPanel() {
            $('#browserErrorContextPanel').addClass('overlay-close').removeClass("overlay-right-open");
            $("#disconnectButton").attr("enabled", "enabled");
        }

        function OpcServerMenu(node) {
            var result = {};
            @if (PermsChecker.HasPermission(Permission.PublishOpcNode))
            {
                @:if (node.original.nodeClass === "Variable" && node.original.publishedNode != true && (node.original.accessLevel & 0x01)) {
                @:  $.extend(result, {
                @:      "Publish": {
                @:          "separator_before": false,
                @:          "separator_after": false,
                @:          "label": "@Strings.NodePublish",
                @:          "nodeId": node.id,
                @:          "nodeName": node.text,
                @:          "action": function (obj) {
                @:             $("#variablePublishContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
                @:             $("#publishHeader").html('@Strings.BrowserContextPanelHeaderPublish');
                @:             $("#publishBody").hide();
                @:             $("#waitingForPublishing").html('@Strings.BrowserContextPanelPublishing');
                @:             $("#rdxLink").hide();
                @:             $("#waitingForPublishing").show();
                @:             $("#connectLoader").show();
                @:             $(".dashboard-cancel-icon").show();
                @:             VariablePublish(obj);
                @:          }
                @:      }
                @:  });
                @:}
            }
            @if (PermsChecker.HasPermission(Permission.PublishOpcNode))
            {
                @:if (node.original.nodeClass === "Variable" && node.original.publishedNode == true && node.original.relevantNode == false && (node.original.accessLevel & 0x01)) {
                @:  $.extend(result, {
                @:      "UnPublish": {
                @:          "separator_before": false,
                @:          "separator_after": false,
                @:          "label": "@Strings.NodeUnpublish",
                @:          "nodeId": node.id,
                @:          "nodeName": node.text,
                @:          "action": function (obj) {
                @:             $("#variablePublishContextPanel").removeClass("overlay-close").addClass("overlay-right-open");
                @:             $("#publishHeader").html('@Strings.BrowserContextPanelHeaderUnpublish');
                @:             $("#publishBody").hide();
                @:             $("#waitingForPublishing").html('@Strings.BrowserContextPanelUnPublishing');
                @:             $("#waitingForPublishing").show();
                @:             $("#connectLoader").show();
                @:             $(".dashboard-cancel-icon").show();
                @:             VariableUnpublish(obj);
                @:          }
                @:      }
                @:  });
                @:}
            }
            if (node.original.nodeClass === "Variable" && (node.original.accessLevel & 0x01)) {
                $.extend(result, {
                    "Read": {
                        "separator_before": false,
                        "separator_after": false,
                        "label": "@Strings.NodeRead",
                        "nodeId": node.id,
                        "nodeName": node.text,
                        "action": function (obj) {
                            VariableRead(obj);
                        }
                    }
                });
            }
            @if (PermsChecker.HasPermission(Permission.ControlOpcServer))
            {
                @:if (node.original.nodeClass === "Variable" && (node.original.accessLevel & 0x02)) {
                @:  $.extend(result, {
                @:      "Write": {
                @:          "separator_before": false,
                @:          "separator_after": false,
                @:          "label": "@Strings.NodeWrite",
                @:          "nodeId": node.id,
                @:          "nodeName": node.text,
                @:          "action": function (obj) {
                @:            VariableWrite(obj);
                @:          }
                @:      }
                @:  });
                @:}
            }
            @if (PermsChecker.HasPermission(Permission.ControlOpcServer))
            {
                @:if (node.original.nodeClass === "Method") {
                @:    $.jstree.reference('#OpcNodeTree').refresh_node(node);
                @:    if (node.original.executable === "True") {
                @:        $.extend(result, {
                @:            "Call": {
                @:                "separator_before": false,
                @:                "separator_after": false,
                @:                "label": "@Strings.NodeCall",
                @:                "nodeId": node.id,
                @:                "nodeName": node.text,
                @:                "action": function (obj) {
                @:                    MethodCall(obj);
                @:                  }
                @:              }
                @:          });
                @:      }
                @:}
            }
        return result;
        }

        var back;
        $(function ($) {
            back = document.referrer;
            $("#hiddenBackUrl").val(back);

            $("#disconnectButton").attr("enabled", "enabled");
            $('#OpcNodeTree').on(
                "init.jstree", function () {
                });
        });

        $(function ($) {
            $('#OpcNodeTree').jstree({
                "plugins": ["wholerow", "contextmenu", "types"],

                "core": {
                    "themes": {
                        "name": "PCS",
                        "dots": false,
                        "icons": true,
                        "stripes": true,
                        "responsive": true
                    },
                    "data": {
                        "type": "POST",
                        "cache": false,
                        "async": false,
                        "url": function (node) {
                            return node.id === "#" ? "/Browser/GetRootNode" : "/Browser/GetChildren";
                        },
                        "data": function (node) {
                            return { __RequestVerificationToken: $('[name=__RequestVerificationToken]').val(), jstreeNode: node.id };
                        },
                        "error": function (xdr) {
                            openErrorPanel();
                            if (xdr.status == 403) {
                                $('#browserErrorMessage').html('@Strings.ErrorInsufficientPermissions');
                            } else if (xdr.status == 500) {
                                $('#browserErrorMessage').html('@Strings.BrowserErrorUnknown');                        
                            } else {
                                $('#browserErrorMessage').html(xdr.responseText);
                            }
                            $('#OpcNodeTree').hide();
                        }
                    }
                },

                "types": {
                    "default": {
                        "icon": "glyphicon"
                    },
                    "published": {
                        "icons": true,
                        "icon": "glyphicon glyphicon-ok"
                    }
                },

                "contextmenu": {
                    "show_at_node": false,
                    "items": OpcServerMenu
                }
            });
        });

        $(function ($) {
            $('#OpcNodeTree').on('ready.jstree', function (e, data) {
                var nodes = $('#OpcNodeTree').jstree('get_json');
                $(this).jstree("open_node", nodes);
            });

            $('#OpcNodeTree').on('open_node.jstree', function (e, data) {
                for (var node in data.instance._model.data) {
                    if (node != "#") {
                        var nodeOriginal = $(this).jstree(true).get_node(node).original;
                        if (nodeOriginal.publishedNode == true) {
                            var jsonNode = $(this).jstree(true).get_node(node)
                            $(this).jstree(true).set_type(jsonNode, "published")
                        }
                        else {
                            var jsonNode = $(this).jstree(true).get_node(node)
                            $(this).jstree(true).set_type(jsonNode, "default")
                        }
                    }
                }
            });
            jQuery('.scrollbar-macosx').scrollbar();
        });
    </script>
}